import _ from 'lodash'
/***
 * 表格组件混入类
 * 抽象标准参数
 */
export const tableMixin = {
  data() {
    return {
      currentEventKey: this.$route.path,
      // 数据字典
      // dictMap: {},
      // 表格分页参数
      pagination: {
        current: 1,
        limit: 10,
      },
      ids: [],
      // 遮罩层
      loading: false,
      totalRecord: 0,
      // 表格数据
      dataList: [],
      finalTableColumns: []
    };
  },
  props: {
    defaultEventKey: {
      type: String,
      default: ''
    },
    // 字典数据
    dictMap: {
      type: Object,
      default: () => {
        return {};
      }
    },
    tableBindColumns: {
      type: Array,
      default: () => {
        return []
      }
    },
    extendColumns: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    /**
     * 表格字段值处理逻辑
     * @param row
     * @param prop
     * @param text
     * @param index
     * @param type
     * @returns {string|*|string}
     */
    tableItemText(row, prop, text, index, type) {
      if (!prop) return '';
      let tempColumn = this.finalTableColumns.find(x => x.prop == prop);
      if (tempColumn && tempColumn.dictCode && this.dictMap[tempColumn.dictCode]) {
        let item = this.dictMap[tempColumn.dictCode].find(x => x.value == text);
        return item ? item.text : '';
      }
      if (tempColumn && tempColumn.formatter) {
        return tempColumn.formatter(row, text, index)
      }
      let childProp = prop.split('.')
      if (childProp && childProp.length > 1) {
        let base = row;
        for (let tempKey of childProp) {
          if (base[tempKey])
            base = base[tempKey];
          else
            return ''
        }
        return _.unescape(base);
      }
      if (type == 'date') {
        text = this.dateTransition(text)
      } else if (type == 'price') {
        text = this.formatSubstring(text)
      } else if (type == 'time') {
        text = this.timeTransition(text)
      } else {
        return _.unescape(text);
      }
      return _.unescape(text);

    },
    getTableExtendParamValue(pCode, pValue, componentType, dictCode = '') {
      if (componentType == 3 || componentType == 4) {

        if (this.dictMap[dictCode]) {
          if (componentType == 3) {
            let item = this.dictMap[dictCode].find(
              (x) => x.value == pValue
            );
            if (item) {
              return item.text;
            }
          } else if (componentType == 4 && pValue) {
            let finalVal = [];
            for (let valArray of pValue.split(",")) {
              let item = this.dictMap[dictCode].find(
                (x) => x.value == valArray
              );
              if (item) {
                finalVal.push(item.text);
              }
            }
            return finalVal.join(",");
          }
        }
        return "";
      }
      return pValue;
    },
    // 日期转换
    dateTransition(value) {
      if (typeof value == "string") {
        if (value.length == 8) {
          let res = value.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");
          return res;
        } else {
          let res = value.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5:$6")
          return res
        }
      }
    },
    // 时间戳转换
    timeTransition(value) {
      return value.replace(/\T/g, " ")
    },
    // 价钱类型的数据千分位分割
    formatSubstring(num) {
      if (typeof num == "string" || typeof num == "number") {
        let arr = (num + "").split(".");
        let int = arr[0] + "";
        let fraction = arr[1] || "";
        let f = int.length % 3;
        let r = int.substring(0, f);
        for (let i = 0; i < Math.floor(int.length / 3); i++) {
          r += "," + int.substring(f + i * 3, f + (i + 1) * 3);
        }
        if (f === 0) {
          r = r.substring(1);
        }
        return r + (!!fraction ? "." + fraction : "");
      }

    },
  },
}
